﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Xml.Linq;
using System.Text;
using System.Xml;
using System.IO;
using SkyDriveAPI;
[DataContract]
public class ImageResult
{
    [DataMember]
    public int num0 { get; set; }
    [DataMember]
    public int num2 { get; set; }
    [DataMember]
    public string str0 { get; set; }
    [DataMember]
    public string str1 { get; set; }
    [DataMember]
    public byte[] Image { get; set; }
}
[ServiceContract]
interface IIntexWPServiceBL
{
    [OperationContract]
    ImageResult GetInfo(string para);
}
public partial class IntexReptServBL : IIntexWPServiceBL
{
    public ImageResult GetInfo(string para)
    {
        ImageResult result = new ImageResult();
        try
        {
            XDocument xdoc = XDocument.Parse(para);
            Dictionary<string, string> dicSrcParamater = new Dictionary<string, string>();
            var items = from item in xdoc.Elements()
                        select item;
            List<XElement> clist = items.ToList<XElement>();
            foreach (XElement item in clist)
            {
                var itemsx = from itemx in item.Elements()
                             select itemx;
                foreach (XElement x in itemsx)
                {
                    dicSrcParamater.Add(x.Name.ToString(), (string)x.Value);
                }
            }
            DataSet set = GetDataSetByQuery(dicSrcParamater);
            string xmlString = "";
            string key = DateTime.Now.ToFileTime().ToString();
            xmlString = DataToXml.Convert2XML("PhoneData",set.Tables[0], key,null,true);            
            result.str0 = xmlString;
        }
        catch (Exception ex)
        {
        }
        return result;
    }
    public DataSet GetDataSetByQuery(Dictionary<string, string> dicSrcParamater)
    {
        DataSet dsResult = new DataSet();
        string table = "Table";
        try
        {
            string commandtype = "CommandText";
            string strCommandType;
            string dbType = "Oracle";
            if (dicSrcParamater.ContainsKey("DBTYPE"))
            {
                dbType = dicSrcParamater["DBTYPE"].ToString();
            }
            OracleCommand catCMD = new OracleCommand();
            OracleParameter catCMDP;
            foreach (KeyValuePair<string, string> kvp in dicSrcParamater)
            {
                switch (kvp.Key)
                {
                    case "DBTYPE":
                        break;
                    case "DataTable":
                        table = (string)dicSrcParamater["DataTable"];
                        break;
                    case "CommandText":
                        catCMD.CommandText = (string)dicSrcParamater["CommandText"];
                        break;
                    case "ConnectionString":
                        string strIntexSystem = (string)dicSrcParamater["ConnectionString"];
                        string OracleDBConnString = "";
                        if (strIntexSystem.ToUpper().Contains("INTEX"))
                        {
                            OracleDBConnString = "user id=intex;data source=" + strIntexSystem + ";password=intex;Unicode=True;" + "Persist Security Info=True;";
                        }
                        catCMD.Connection = new OracleConnection(OracleDBConnString);
                        if (catCMD.Connection.State == ConnectionState.Closed)
                            catCMD.Connection.Open();
                        break;
                    case "CommandType":
                        strCommandType = (string)dicSrcParamater["CommandType"];
                        switch (strCommandType)
                        {
                            case "Text":
                                catCMD.CommandType = CommandType.Text;
                                commandtype = "CommandText";
                                break;
                            case "StoredProcedure":
                                catCMD.CommandType = CommandType.StoredProcedure;
                                commandtype = "StoredProcedure";
                                break;
                            case "TableDirect":
                                catCMD.CommandType = CommandType.TableDirect;
                                commandtype = "TableDirect";
                                break;
                            case "BatchJob":
                                catCMD.CommandType = CommandType.Text;
                                commandtype = "BatchJob";
                                break; 
                            default:
                                catCMD.CommandType = CommandType.Text;
                                break;
                        }
                        break;
                    default:
                        catCMDP = new OracleParameter();
                        catCMDP.ParameterName = kvp.Key;
                        catCMDP.Value = (string)kvp.Value;
                        catCMD.Parameters.Add(catCMDP);
                        break;
                }
            }
            if (commandtype == "BatchJob")
            {
                switch (catCMD.CommandText)
                {
                    case "BatchLoadIntexKeyTable":
                        dsResult=BATCHUPLOAD.BatchLoadIntexKeyTable();
                        break;
                    case "GetSkyDriveFileList":
                        dsResult = BATCHUPLOAD.GetSkyDriveFileList();
                        break;
                    default:
                        break;
                }
                return dsResult;
            }
                     
            OracleDataAdapter myDa = new OracleDataAdapter(catCMD);
            myDa.Fill(dsResult);
            if (dsResult.Tables.Count > 0)
            {
                dsResult.Tables[0].TableName = table;
            }
        }
        catch (Exception ex)
        {
            System.Console.Write(ex.StackTrace, ex);

        }
        return dsResult;
    }
}
